package com.adb.receivecontent.uc;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

import com.adb.receivecontent.R;
import com.adb.receivecontent.bean.LinkMagnetBean;

public class ReceiveContentAdapter extends BaseAdapter {
	
	public static final String TAG = ReceiveContentAdapter.class.getCanonicalName();
	
	private LayoutInflater mLayoutInflater;
	private ItemRadionListener mRadioButtonCallback;
	private ArrayList<Boolean> mCheckableList;
	public static LinkedList<LinkMagnetBean> mContentArrayList;
	
	public ReceiveContentAdapter(Context mCntext){
		mLayoutInflater = LayoutInflater.from(mCntext);
		mRadioButtonCallback = (ItemRadionListener)mCntext;
		mContentArrayList = new LinkedList<LinkMagnetBean>();
		mCheckableList = new ArrayList<Boolean>();
	}
	
	public void checkableItemInList(int location){
		notifyDataSetChanged();
	}
	
	public synchronized void addLinksItems(ArrayList<LinkMagnetBean> mArrayList){
		
		
		if(!mContentArrayList.isEmpty()){
			mContentArrayList.clear();
		}
		
		for(LinkMagnetBean mTempBean : mArrayList){
			mContentArrayList.add(mTempBean);
		}
		for(int i=0;i<mContentArrayList.size();i++){
			mCheckableList.add(false);
		}
		notifyDataSetChanged();
	
	}
	
	
	@Override
	public int getCount() {
		return mContentArrayList.size();
	}
	
	@Override
	public Object getItem(int position) {
		return mContentArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		CatchViewHolder mViewHolder;
		
		if(convertView == null){
			
			mViewHolder = new CatchViewHolder(); 
			
			convertView = mLayoutInflater.inflate(R.layout.row_send_content_items, parent, false);
			mViewHolder.mTextViewLinkName = (TextView)convertView.findViewById(R.id.textview_radio_button);
			mViewHolder.mRadioButtonLinkName = (RadioButton)convertView.findViewById(R.id.btn_radion_link_name);
			
			convertView.setTag(mViewHolder);
					
		}else{
			
			mViewHolder = (CatchViewHolder)convertView.getTag();
			
		}
		
		LinkMagnetBean mTempBean = mContentArrayList.get(position);
		
		if(mTempBean != null){
			
			if(mViewHolder != null){
				
				mViewHolder.mTextViewLinkName.setText(mTempBean.getStrlinkName());
				
				if(mCheckableList.get(position)){
					mViewHolder.mRadioButtonLinkName.setChecked(true);	
				}else{
					
					mViewHolder.mRadioButtonLinkName.setChecked(false);	
				}
				
				mViewHolder.mRadioButtonLinkName.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						
						for(int i=0;i<mCheckableList.size();i++){
							if(i == position){
								mCheckableList.set(position,true);	
							}else{
								mCheckableList.set(i,false);
							}
						}
						mRadioButtonCallback.onRadioItemLinkClick(isChecked,position);
					}
				});	
			}
			
		}
		
		return convertView;
	}
	
	
	class CatchViewHolder{
		
		TextView mTextViewLinkName;
		RadioButton mRadioButtonLinkName;
		
	}
	
}
